package club.calong.sanniu.controller;

import club.calong.sanniu.http.response.Response;
import club.calong.sanniu.pojo.Video;
import club.calong.sanniu.util.Utils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/image")
public class ImageController {

    @Value("${app.host}")
    String APP_HOST;

    @Value("${app.image.path}")
    String IMAGE_PATH;


    @PostMapping
    public Response save (@RequestParam("image") MultipartFile file, String url) {
        checkResourcesDirExits();
        System.out.println( file.getOriginalFilename());
        if (file != null && file.getOriginalFilename() != null) { 
            File image;
            String fileName;
            if (url != null && url.startsWith(APP_HOST)) {
                fileName = url.replace(APP_HOST + "res/image/", IMAGE_PATH);
            } else {
                String[] originalName = file.getOriginalFilename().split("\\.");
                String suffix = originalName[originalName.length - 1];
                fileName = Utils.generateFileName(IMAGE_PATH, suffix);
            }
            image = new File(fileName);
            try {
                System.out.println("related path is :" + fileName);
                System.out.println("real path is :" + image.getAbsolutePath());
                file.transferTo(image.getAbsoluteFile());
                String _url;
                if (url != null && url.startsWith(APP_HOST)) {
                    _url = url;
                } else {
                    _url = APP_HOST + "res/image/" + fileName.substring(fileName.lastIndexOf("/") + 1);
                }

                System.out.println("return url is :" + _url);
                Video video = new Video("图片", _url);
                video.setSize(file.getSize());
                return new Response(Response.SUCCESS, Response.Message.UPLOAD_OK, video);
            } catch (IOException e) {
                e.printStackTrace();
                return new Response(Response.FAILURE, Response.Message.UPLOAD_ERROR, null);
            }
        }
        return new Response(Response.FAILURE, Response.Message.UPLOAD_ERROR, null);
    }

    public void checkResourcesDirExits () {
        File   file=new   File(".");
        String path=file.getAbsolutePath();
        System.out.println("current dir is :" + path);
        File publichdir = new File("./public");
        File videodir = new File("./public/video");
        File imagedir = new File("./public/image");
        if(!(publichdir.exists() && publichdir.isDirectory())){
            publichdir.mkdir();
        }
        if(!(videodir.exists() && videodir.isDirectory())){
            videodir.mkdir();
        }
        if(!(imagedir.exists() && imagedir.isDirectory())){
            imagedir.mkdir();
        }
        path=file.getPath();
    }
}
